package jgl.vajra.management.part.mine.pop;

import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.management.R;
import jgl.vajra.management.bean.CommissionDetailsSortBean;
import jgl.vajra.management.databinding.PopCommissionDetailsBinding;
import jgl.vajra.management.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.management.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.management.widget.utils.UIUtils;

public class CommissionDetailsPop implements BaseBindingItemPresenter<CommissionDetailsSortBean> {

    private FragmentActivity activity;
    private String orderBy;
    private View mPopRootView;
    private PopupWindow menuPop;
    private CommissionDetailsListener commissionDetailsListener;

    public CommissionDetailsPop(FragmentActivity activity) {
        this.activity = activity;
    }


    private void initPopupWindow() {
        mPopRootView = View.inflate(UIUtils.getContext(), R.layout.pop_commission_details, null);
        PopCommissionDetailsBinding mBinding = DataBindingUtil.bind(mPopRootView);

        menuPop = new PopupWindow(mPopRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuPop.setOutsideTouchable(false);
        menuPop.setBackgroundDrawable(new BitmapDrawable());
        menuPop.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        menuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //popupwindow消失的时候恢复成原来的透明度
                backgroundAlpha(1f);
            }
        });
        mBinding.setView(this);

        List<CommissionDetailsSortBean> list=new ArrayList<>();
        list.add(new CommissionDetailsSortBean("按时间顺序排序","",false));
        list.add(new CommissionDetailsSortBean("按用户名排序","order by nickname",false));
        list.add(new CommissionDetailsSortBean("按手机号排序","order by phone",false));

        for (int i = 0; i < list.size(); i++) {
            Logger.d(orderBy+"==数据啊="+list.get(i).itemOrderBy);
            if (orderBy==list.get(i).itemOrderBy){
                list.get(i).isClick=true;
            }
        }

        //机械类型
        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(activity, list, R.layout.pop_item_commission_details);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);
    }

    public void showPopupWindow(View v) {
        if (menuPop == null)
            initPopupWindow();
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        backgroundAlpha(0.5f);

//        menuPop.showAtLocation(v, Gravity.NO_GRAVITY, (int) (location[0] - DisplayUtils.getDimension(R.dimen.dp_4)), (int) (location[1] - mPopRootView.getMeasuredHeight() - DisplayUtils.getDimension(R.dimen.dp_4)));  //显示指定控件的上方
        menuPop.showAsDropDown(v, 0, 0);    //显示指定控件的下方
    }

    public boolean isShowing() {
        return menuPop != null && menuPop.isShowing();
    }

    public void dismiss() {
        if (menuPop != null && menuPop.isShowing())
            menuPop.dismiss();
    }
    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (activity == null) return;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        activity.getWindow().setAttributes(lp);

    }

    @Override
    public void onItemClick(int position, CommissionDetailsSortBean itemData) {
        if (commissionDetailsListener != null) {
            commissionDetailsListener.onDetermine(position,itemData.itemOrderBy);
        }
        dismiss();
    }

    public void setCommissionDetailsListener(CommissionDetailsListener listener) {
        commissionDetailsListener = listener;

    }

    public interface CommissionDetailsListener {
        void onDetermine(int itemData,String name);
    }

    public void setData(String orderBy){
        this.orderBy = orderBy;
    }
}

